<?php

namespace App\Http\Resources\Article;

use App\Http\Resources\BaseResource;
use App\Models\Article\ArticleCategoryMappingsModel;
use App\Models\Article\ArticleModel;
use App\Service\Article\ArticleService;
use Illuminate\Http\Resources\MissingValue;

class ArticleDetailResource extends BaseResource
{
    public function toArray($request): array
    {
        return [
            "id"                  => $this->id,
            "type"                => $this->type,
            "subTitle"            => $this->subTitle,
            "title"               => $this->title,
            "description"         => $this->description,
            "coverImgUrl"         => $this->coverImgUrl,
            "previewUrl"          => json_decode($this->previewUrl, true),
            "content"             => ArticleService::replaceTranscodingContent(
                $this->content,
                $this->transcodingVideoDetail
            ),
            "supportNum"          => $this->supportNum,
            "collectNum"          => $this->collectNum,
            "forwardNum"          => $this->forwardNum,
            "readNum"             => $this->readNum,
            "showNum"             => $this->showNum,
            "formatEffectiveTime" => formatDate($this->effectiveTime),
            "effectiveTime"       => $this->effectiveTime,
            "status"              => $this->status,
            "statusText"          => translateEnumCodeToCn(ArticleModel::STATUS_TEXT, $this->status, "-"),
            "publishType"         => $this->publishType,
            "publishTypeText"     => translateEnumCodeToCn(ArticleModel::PUBLISH_TYPE_TEXT, $this->publishType, "-"),
            "isCollect"           => $this->when(
                $this->resource->hasAttribute("isCollect"),
                $this->isCollect,
                new MissingValue()
            ),
            "isSupport"           => $this->when(
                $this->resource->hasAttribute("isSupport"),
                $this->isSupport,
                new MissingValue()
            ),
            "sort"                => $this->sort,
            "putTop"              => $this->putTop,
            "createUsername"      => attributeGet($this, "createUserRel.nickname", "-"),
            "shareUrl"            => config('open-shop.m') . "content/detail/" . $this->id,
            "extra"               => $this->extra,
            "categoryDetail"      => $this->whenLoaded(
                "articleCategoryMappingsRel",
                function () {
                    return $this->getCategoryDetail();
                },
                new MissingValue()
            )
        ]; // TODO: Change the autogenerated stub
    }

    /**
     * @return mixed|null
     */
    public function getCategoryDetail()
    {
        $articleCategoryMappingsRel = attributeGet($this, "articleCategoryMappingsRel", null);
        if ($articleCategoryMappingsRel) {
            $first = $articleCategoryMappingsRel->filter(function (
                ArticleCategoryMappingsModel $articleCategoryMappingsModel
            ) {
                if (attributeGet($articleCategoryMappingsModel, "articleCategoryRel", null)) {
                    return true;
                }
                return false;
            })->values()->sortBy("articleCategoryRel.sort")->first();
            return attributeGet($first, "articleCategoryRel", null);
        }
        return null;
    }
}
